package com.example.startscreen;

import java.text.Format;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.AndroidCharacter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.startscreen.R;
import com.example.startscreen.MainActivity.languageEnum;
import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;
import com.jakewharton.android.viewpagerindicator.TitleProvider;

// 	METODA KOJA PODESAVA NASLOVE U HEADERU
public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {

	public static MainActivity mainActivity;

	public static String[] titles = new String[] { "Home", "Map mode", "Compass", "Quicky Photo" };
	public static String[] naslovi = new String[] { "Početna", "Online mapa", "Kompas", "Brza Fotka" };
	public static String[] most;

	private final Context context;
	public int page;

	public ViewPagerAdapter(Context context, MainActivity mainActivity) {
		this.context = context;
		this.mainActivity = mainActivity;
	}

	public String getTitle(int position) {
		return titles[position];
	}

	@Override
	public int getCount() {
		return titles.length;
	}

	// METODA KOJA DODJELJUJE VRIJEDNOSTI LAYOUTA XML.. ILI DINAMICKE..
	// OVDJE DODAVATI BUTTONE I KREIRATI INTENTE...
	@Override
	public View instantiateItem(View collection, int position) { // prvi
																	// argument
																	// je bio
																	// pager
		// TextView v = new TextView(context);
		// v.setText(titles[position]);
		// ((ViewPager) pager).addView(v, 0);
		// return v;
		LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		int resId = 0;
		switch (position) {

		case 0:
			resId = R.layout.middle;

			View viewMiddle = (TableLayout) inflater.inflate(resId, null);
			((ViewPager) collection).addView(viewMiddle, 0);

			TableLayout layoutMiddle = (TableLayout) viewMiddle.findViewById(R.id.middleTableLayout);

			
			// podesavanje header-a
			// StartScreenActivity.indicatorMain.setBackgroundResource(R.drawable.headeryellow4);

			page = 2;
			return viewMiddle;
			// break;
		case 1:
			resId = R.layout.right;

			// omogucava kreiranje buttona, layout-a... u okviru trenutnog viewa
			// koji se generise

			View viewright = (FrameLayout) inflater.inflate(resId, null);

			FrameLayout layout = (FrameLayout) viewright.findViewById(R.id.FrameLayout1);
			final Button enterButton = (Button) layout.findViewById(R.id.enterButton);
			enterButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					playNotificationSound();
					//enterButton.setBackgroundResource(R.drawable.storybuttononclick);
					//enterButton.setBackgroundResource(R.drawable.storybutton);

					Intent i = new Intent(context, OnlineMapActivity.class);
					i.putExtra("whatToDo", OnlineMapActivity.mapmodeEnum.DRAW_ALL_MUNICIPALS.getValue());
					context.startActivity(i);
					// Toast.makeText(context,
					// "Language info: "+MainActivity.language,
					// Toast.LENGTH_SHORT).show();

				}
			});
			// text podesavanje
			TextView mapText = (TextView) layout.findViewById(R.id.mapTextView);
			mapText.setTextColor(Color.BLACK);

			// podesavanje header-a
			// StartScreenActivity.indicatorMain.setBackgroundResource(R.drawable.headerbrown);

			((ViewPager) collection).addView(viewright, 0);

			page = 2;
			return viewright;
			//

		
		case 2:
			resId = R.layout.compassentrance;
			View view3 = (LinearLayout) inflater.inflate(resId, null);
			((ViewPager) collection).addView(view3, 0);

			LinearLayout linlayout3 = (LinearLayout) view3.findViewById(R.id.compassEntrance2);

			final Button offlineListButton8 = (Button) linlayout3.findViewById(R.id.offlineListButton8);
			offlineListButton8.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Intent i = new Intent(context, BitmapPositioningActivity.class);
					context.startActivity(i);
				}
			});
			page = 3;
			return view3;
			// break;

		case 3:
			resId = R.layout.farright;
			View view2 = (LinearLayout) inflater.inflate(resId, null);
			((ViewPager) collection).addView(view2, 0);

			LinearLayout linlayout = (LinearLayout) view2.findViewById(R.id.LinearLayout10);

			final Button offlineListButton = (Button) linlayout.findViewById(R.id.offlineListButton);
			offlineListButton.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Intent i = new Intent(context, CameraActivity.class);
					context.startActivity(i);
				}
			});

			page = 3;
			return view2;
			// break;
			

		}
		

		return null;

	}

	@Override
	public void destroyItem(View pager, int position, Object view) { // odradjena
																		// izmjena..
																		// distoy
																		// se
																		// vrsio
																		// nad
																		// VIEW
		((ViewPager) pager).removeView((View) view); // objektima a ne TextView
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void finishUpdate(View view) {
	}

	@Override
	public void restoreState(Parcelable p, ClassLoader c) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View view) {
	}

	public void playNotificationSound() {
		//		Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		//		Ringtone r = RingtoneManager.getRingtone(context, notification);
		//		r.play();	

		final MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.click3);
		mediaPlayer.start(); // no need to call prepare(); create() does that for you

	}
}